/*
 * NPFNewFeedDialog.java
 *
 * Created on March 18, 2007, 11:24 PM
 */

package newpodfeed;

import javax.swing.JComponent;
import java.util.*;
import java.io.*;

/**
 *
 * @author  newpodfeed
 */
public class NPFNewFeedDialog extends javax.swing.JDialog {
    
    Vector <JComponent>vector;
    NPFTextDialog dialog;
    NPFDate date;
    
    /** Creates new form NPFNewFeedDialog */
    public NPFNewFeedDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        vector = new Vector();
        vector.add(explicitCheckBox);
        vector.add(jLabel6);
        vector.add(keywordsField);
        vector.add(jLabel7);
        vector.add(authorField);
        vector.add(jLabel8);
        vector.add(emailField);
        dialog = new NPFTextDialog(NPFMainFrame.frame, true);
        date = new NPFDate();
        dialog.clear();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        titleField = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        linkField = new javax.swing.JTextField();
        descriptionField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        copyrightField = new javax.swing.JTextField();
        categoryField = new javax.swing.JTextField();
        iTunesCheckBox = new javax.swing.JCheckBox();
        explicitCheckBox = new javax.swing.JCheckBox();
        jLabel6 = new javax.swing.JLabel();
        keywordsField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        authorField = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        emailField = new javax.swing.JTextField();
        generateButton = new javax.swing.JButton();
        closeNewFeedButton = new javax.swing.JButton();
        imageLabel = new javax.swing.JLabel();
        imageField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("New Feed");

        jLabel1.setText("Title");

        titleField.setColumns(20);
        titleField.setToolTipText("Title of the podcast");

        jLabel2.setText("Website");

        jLabel3.setText("Description");

        linkField.setColumns(20);
        linkField.setToolTipText("Link to the podcast's website");

        descriptionField.setColumns(20);
        descriptionField.setToolTipText("Description of the podcast");

        jLabel4.setText("Copyright");

        jLabel5.setText("Category");

        copyrightField.setColumns(20);
        copyrightField.setToolTipText("Podcast's copyright statement");

        categoryField.setColumns(20);
        categoryField.setToolTipText("Category of the podcast, such as Comedy etc.");

        iTunesCheckBox.setText("iTunes");
        iTunesCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        iTunesCheckBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                iTunesCheckBoxActionPerformed(evt);
            }
        });

        explicitCheckBox.setText("Explicit");
        explicitCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        explicitCheckBox.setEnabled(false);

        jLabel6.setText("Keywords");
        jLabel6.setEnabled(false);

        keywordsField.setColumns(20);
        keywordsField.setToolTipText("Keywords and tags for the podcast");
        keywordsField.setEnabled(false);

        jLabel7.setText("Author");
        jLabel7.setEnabled(false);

        authorField.setColumns(20);
        authorField.setToolTipText("Creator of the podcast");
        authorField.setEnabled(false);

        jLabel8.setText("E-mail");
        jLabel8.setEnabled(false);

        emailField.setColumns(20);
        emailField.setToolTipText("Podcast creator's e-mail address");
        emailField.setEnabled(false);

        generateButton.setText("Generate");
        generateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generateButtonActionPerformed(evt);
            }
        });

        closeNewFeedButton.setText("Close");
        closeNewFeedButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                closeNewFeedButtonActionPerformed(evt);
            }
        });

        imageLabel.setText("Image");

        imageField.setColumns(20);
        imageField.setToolTipText("Location on the web of the image file for the podcast's artwork");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(linkField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(copyrightField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(categoryField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(iTunesCheckBox)
                    .addComponent(explicitCheckBox)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(keywordsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(authorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(generateButton)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(imageLabel))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(imageField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(descriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(closeNewFeedButton)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(titleField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(linkField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(descriptionField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(imageLabel)
                    .addComponent(imageField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(copyrightField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(categoryField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(iTunesCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(explicitCheckBox)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(keywordsField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(authorField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(emailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(generateButton)
                    .addComponent(closeNewFeedButton))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void iTunesCheckBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_iTunesCheckBoxActionPerformed
        JComponent component;
        boolean enabled;
        if (iTunesCheckBox.isSelected()) {
            enabled = true;
        }
        else {
            enabled = false;
        }
        for(int l = 0; l < vector.size(); l++) {
            component = vector.get(l);
            component.setEnabled(enabled);
        }
    }//GEN-LAST:event_iTunesCheckBoxActionPerformed

    private void closeNewFeedButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeNewFeedButtonActionPerformed
        setVisible(false);
    }//GEN-LAST:event_closeNewFeedButtonActionPerformed

    private void generateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generateButtonActionPerformed
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        NPFFeedFileFilter feedFilter = new NPFFeedFileFilter();
        NPFConfirmDialog confirm = new NPFConfirmDialog(NPFMainFrame.frame, true);
        NPFDialog feedback = new NPFDialog(NPFMainFrame.frame, true);
        File file;
        int returnType = 0;
        
        chooser.setFileFilter(feedFilter);
        
        returnType = chooser.showSaveDialog(this);
        
        if (returnType == javax.swing.JFileChooser.CANCEL_OPTION) {
            return;
        }
        
        file = chooser.getSelectedFile();
        
        if (file.getName().endsWith(".xml") != true) {
            String fileName = file.getPath();
            fileName += ".xml";
            file = new File(fileName);
        }
        
        if (file.exists()) {
            confirm.setVisible(true);
            
            int confirmStatus = confirm.getReturnStatus();
            
            if (confirmStatus == confirm.RET_CANCEL) {
                return;
            }
        }
        else {
            try {
                file.createNewFile();
            }
            catch(Exception e) {
                feedback.clear();
                feedback.append("Error creating new file.\n\n");
                feedback.append("Error log is as follows:\n\n");
                feedback.append("" + e + "\n\n");
                feedback.append("Errors can be reported to newpodfeed.googlecode.com");
                feedback.setVisible(true);
                return;
            }
        }
        
        try {
            BufferedWriter feedWriter = new BufferedWriter(new FileWriter(file));
            
            feedWriter.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
            //feedWriter.write("<!-- npf -->\n");
            feedWriter.write("<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\">\n");
            feedWriter.write("<!-- npf -->\n");
            feedWriter.write("\n");
            feedWriter.write("<!-- Generated by NewPodFeed, a free open-source application that\n");
            feedWriter.write("generates the XML RSS feed for podcasts.\n");
            feedWriter.write("Get it from http://newpodfeed.googlecode.com -->\n");
            feedWriter.write("\n");
            feedWriter.write("<channel>\n");
            feedWriter.write("\n");
            feedWriter.write("<title>" + titleField.getText() + "</title>\n");
            feedWriter.write("<link>" + linkField.getText() + "</link>\n");
            feedWriter.write("<description>" + descriptionField.getText() + "</description>\n");
            feedWriter.write("<image>\n");
            feedWriter.write("<title>" + titleField.getText() + "</title>\n");
            feedWriter.write("<link>" + linkField.getText() + "</link>\n");
            feedWriter.write("<url>" + imageField.getText() + "</url>\n");
            feedWriter.write("</image>\n");
            if (iTunesCheckBox.isSelected()) {
                feedWriter.write("<itunes:image href=\"" + imageField.getText() + "\" />\n");
            }
            feedWriter.write("<language>en-us</language>\n");
            feedWriter.write("<copyright>" + copyrightField.getText() + "</copyright>\n");
            if (iTunesCheckBox.isSelected()) {
                if (explicitCheckBox.isSelected()) {
                    feedWriter.write("<itunes:explicit>Yes</itunes:explicit>\n");
                }
                else {
                    feedWriter.write("<itunes:explicit>No</itunes:explicit>\n");
                }
            }
            feedWriter.write("<category>" + categoryField.getText() + "</category>\n");
            feedWriter.write("<lastBuildDate>" + date + "</lastBuildDate>\n");
            feedWriter.write("<ttl>60</ttl>\n");
            if (iTunesCheckBox.isSelected() != true) {
                feedWriter.write("\n");
                feedWriter.write("<!-- item insert point -->\n");
            }
            feedWriter.write("\n");
            if (iTunesCheckBox.isSelected()) {
                feedWriter.write("<itunes:summary>" + descriptionField.getText() + "</itunes:summary>\n");
                feedWriter.write("<itunes:category text=\"" + categoryField.getText() + "\" />\n");
                /* Keywords have been deprecated by iTunes */
                //feedWriter.write("<itunes:keywords>" + keywordsField.getText() + "</itunes:keywords>\n");
                feedWriter.write("<itunes:author>" + authorField.getText() + "</itunes:author>\n");
                feedWriter.write("<managingEditor>" + emailField.getText() + "</managingEditor>\n");
                feedWriter.write("<itunes:owner>\n");
                feedWriter.write("<itunes:name>" + authorField.getText() + "</itunes:name>\n");
                feedWriter.write("<itunes:email>" + emailField.getText() + "</itunes:email>\n");
                feedWriter.write("</itunes:owner>\n");
                feedWriter.write("\n");
                feedWriter.write("<!-- item insert point -->\n");
                feedWriter.write("\n");
            }
            feedWriter.write("</channel>\n");
            feedWriter.write("\n");
            feedWriter.write("</rss>\n");
            
            feedWriter.flush();
            feedWriter.close();
        }
        catch(Exception e) {
            feedback.clear();
            feedback.append("There's been an error while creating the new feed.\n\n");
            feedback.append("The error log is as follows:\n\n");
            feedback.append("" + e + "\n\n");
            feedback.append("Errors can be reported to newpodfeed.googlecode.com");
            feedback.setVisible(true);
            return;
        }
        
        feedback.clear();
        feedback.setText("New feed " + file.getName() + " has been created.");
        feedback.setVisible(true);
        setVisible(false);
        
        /*dialog.clear();
        dialog.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>");
        dialog.println("<rss xmlns:itunes=\"http://www.itunes.com/dtds/podcast-1.0.dtd\" version=\"2.0\">");
        dialog.println("");
        dialog.println("<!-- Generated by NewPodFeed, a free open-source application that");
        dialog.println("generates the XML RSS feed for podcasts.");
        dialog.println("Get it from http://newpodfeed.googlecode.com -->");
        dialog.println("");
        dialog.println("<channel>");
        dialog.println("");
        dialog.println("<title>" + titleField.getText() + "</title>");
        dialog.println("<link>" + linkField.getText() + "</link>");
        dialog.println("<description>" + descriptionField.getText() + "</description>");
        dialog.println("<image>");
        dialog.println("<title>" + titleField.getText() + "</title>");
        dialog.println("<link>" + linkField.getText() + "</link>");
        dialog.println("<url>" + imageField.getText() + "</url>");
        dialog.println("</image>");
        if (iTunesCheckBox.isSelected()) {
            dialog.println("<itunes:image href=\"" + imageField.getText() + "\" />");
        }
        dialog.println("<language>en-us</language>");
        dialog.println("<copyright>" + copyrightField.getText() + "</copyright>");
        if (iTunesCheckBox.isSelected()) {
            if (explicitCheckBox.isSelected()) {
                dialog.println("<itunes:explicit>Yes</itunes:explicit>");
            }
            else {
                dialog.println("<itunes:explicit>No</itunes:explicit>");
            }
        }
        dialog.println("<category>" + categoryField.getText() + "</category>");
        dialog.println("<lastBuildDate>" + date + "</lastBuildDate>");
        dialog.println("<ttl>60</ttl>");
        dialog.println("");
        if (iTunesCheckBox.isSelected()) {
            dialog.println("<itunes:summary>" + descriptionField.getText() + "</itunes:summary>");
            dialog.println("<itunes:category text=\"" + categoryField.getText() + "\" />");
            dialog.println("<itunes:keywords>" + keywordsField.getText() + "</itunes:keywords>");
            dialog.println("<itunes:author>" + authorField.getText() + "</itunes:author>");
            dialog.println("<managingEditor>" + emailField.getText() + "</managingEditor>");
            dialog.println("<itunes:owner>");
            dialog.println("<itunes:name>" + authorField.getText() + "</itunes:name>");
            dialog.println("<itunes:email>" + emailField.getText() + "</itunes:email>");
            dialog.println("</itunes:owner>");
            dialog.println("");
        }
        dialog.println("</channel>");
        dialog.println("");
        dialog.println("</rss>");
        dialog.setVisible(true);*/
    }//GEN-LAST:event_generateButtonActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NPFNewFeedDialog(new javax.swing.JFrame(), true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField authorField;
    private javax.swing.JTextField categoryField;
    private javax.swing.JButton closeNewFeedButton;
    private javax.swing.JTextField copyrightField;
    private javax.swing.JTextField descriptionField;
    private javax.swing.JTextField emailField;
    private javax.swing.JCheckBox explicitCheckBox;
    private javax.swing.JButton generateButton;
    private javax.swing.JCheckBox iTunesCheckBox;
    private javax.swing.JTextField imageField;
    private javax.swing.JLabel imageLabel;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField keywordsField;
    private javax.swing.JTextField linkField;
    private javax.swing.JTextField titleField;
    // End of variables declaration//GEN-END:variables
    
}
